<?php
/*
  [EWEISHOP] Copyright(C) 2017 Elinkint Inc.
  This is NOT a freeware, use is subject to license terms
   
  PaymentBill.php 2018-05-24 18:30 cunx $
   
*/

namespace common\library\wechat\payment;

use common\component\wechat\Wechat;
use common\library\wechat\WechatApi;
use yii\base\Exception;

/**
 * 微信对账单
 * Class PaymentBill
 * @package common\library\wechat\payment
 * @author cunx
 */
class PaymentBill extends WechatApi
{
    static $apiType = 'payment';
    static $paymentId = 0;

    /**
     * 下载对账单
     * @param string $billDate 账单的日期 格式：20140603
     * -------------------------------------------------------------------------------
     * @param $billType 账单类型，可能的值：ALL/SUCCESS/REFUND/RECHARGE_REFUND
     * ALL，返回当日所有订单信息，默认值；SUCCESS，返回当日成功支付的订单；
     * REFUND，返回当日退款订单；RECHARGE_REFUND，返回当日充值退款订单
     * -------------------------------------------------------------------------------
     * @return array|bool|void
     * @author cunx
     */
    public static function get(string $billDate, string $billType)
    {
        try {
            $result = self::getInstance()->bill->get($billDate, $billType);
        } catch (\Exception $result) {
        }
        return Wechat::apiError($result);
    }

    /**
     * 保存对账单为文件
     * @param string $billDate 账单的日期 格式：20140603
     * -------------------------------------------------------------------------------
     * @param $billType 账单类型，可能的值：ALL/SUCCESS/REFUND/RECHARGE_REFUND
     * ALL，返回当日所有订单信息，默认值；SUCCESS，返回当日成功支付的订单；
     * REFUND，返回当日退款订单；RECHARGE_REFUND，返回当日充值退款订单
     * -------------------------------------------------------------------------------
     * @param string $path 文件存储路径 格式：'your/path'
     * @param string $fileName 文件名称，需要包含拓展名
     * @return array|bool|void
     * @author cunx
     */
    public static function save(string $billDate, string $billType, string $path, string $fileName)
    {
        try {
            $bill = self::get($billDate, $billType);
            if (is_object($bill)) {
                $result = $bill->saveAs($path, $fileName);
            } else {
                throw new Exception('文件存储失败',-1);
            }
        } catch (\Exception $result) {
            return false;
        }
        return true;
    }

}